﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;
using System.Collections.Generic;

public partial class admin_spgl_tjslt : System.Web.UI.Page
{
    private const string _folderUrl = "files/movie_images/";
    GetMovie gm = new GetMovie();
    public int pagesize = 13;
    public int pages = 0;
    public int page = 1;
    public int prePage = 1;
    public int forPage = 1;
    public int dataTotal = 0;
    public string page_url;
    public bool bl = false;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin_id"] == null)
        {
            Response.Redirect("admin_logout.aspx");
            return;
        }
        if (!IsPostBack)
        {
            if (Request["page"] != null)
            {
                string p = Request["page"];
                page = int.Parse(p);
            }
            else
            {
                page = 1;
            }
            ViewState.Add("page", page);
            page_url = Request.Url.ToString().Split('?')[0];
            controlRepeater();
        }
    }

    private void controlRepeater()
    {
        if (ViewState["page"] != null)
        {
            page = (int)ViewState["page"];
        }
        DataTable table1 = gm.Get_All_Movie();
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = table1.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = pagesize;
        pages = pds.PageCount;
        dataTotal = table1.Rows.Count;
        page = page < 1 ? 1 : page;
        page = page > pages ? pages : page;
        forPage = page + 1;
        prePage = page - 1;
        pds.CurrentPageIndex = page - 1;
        this.repeater1.DataSource = pds;
        this.repeater1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)((Control)sender).Parent.FindControl("tbId");
        int id = int.Parse(tb.Text);
        FileUpload upload = (FileUpload)((Control)sender).Parent.FindControl("FileUpload1");
        string LocalPath = Server.MapPath(_folderUrl); // 上传路径  
        string fe = System.IO.Path.GetExtension(upload.FileName).ToLower();
        string fn = LocalPath + id.ToString() + fe;
        string fn_x = _folderUrl + id.ToString() + fe;
        List<string> extList = new List<string>(new string[] { ".gif", ".png", ".jpeg", ".jpg", ".bmp" });
        if (extList.Contains(fe))
        {
            upload.PostedFile.SaveAs(fn);
            GetMovie gm = new GetMovie();
            if (gm.AddImage(id, fn_x))
            {
                this.lbMsg.Text = "上传成功，且修改数据库";
                controlRepeater();
            }
            else
            {
                this.lbMsg.Text = "上传成功，但修改数据库失败";
            }
        }
        else
        {
            this.lbMsg.Text = "上传文件格式不正确，请选择图片格式";
        }
    }

    protected string checkout(bool bl)
    {
        if (bl)
        {
            return "是";
        }
        else
        {
            return "否";
        }
    }
}